import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import store from './store'
import i18n from './i18n'
import api from './api/api'

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.axios = axios
Vue.prototype.$api = api

// 添加全局token
if (sessionStorage.getItem('USER')) {
  const sessionToken = JSON.parse(sessionStorage.getItem('USER'))
  const token = JSON.parse(JSON.stringify(sessionToken.token))
  if (token) {
    Vue.prototype.axios.defaults.headers['Authorization'] = token
  } else {
    Vue.prototype.axios.defaults.headers['Authorization'] = null
  }
}

Vue.prototype.axios.interceptors.request.use(function (config) {
  // console.log(config);
  if (sessionStorage.getItem('USER')) {
    const sessionToken = JSON.parse(sessionStorage.getItem('USER'))
    const token = JSON.parse(JSON.stringify(sessionToken.token))
    if (token) {
      config.headers['Authorization'] = token
    } else {
      config.headers['Authorization'] = null
    }
    config.headers['Language'] = sessionStorage.getItem('lan')
  }
  return config
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App),
  components: { App },
  template: '<App/>'
})
